import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.util.*

/**
 * Created by Administrator on 2019/05/30.
 */

fun main(args: Array<String>) {
    val a = 12
    println("sum is " + sum(1,a))
    var s = 1
    val s1 = "a is $s";
    println(s1)

    // 模板中的任意表达式：
    val s2 = "${s1.replace("is", "was")}, but now is $s"
    println(s2)

    println(maxOf2(2, 5))

    println(getStrLen("asd"))

    testLoop()

    println(whenExp(123L))

    println(isOnRange(23))

    testIteration()

    println(parseInt("123"))

    val items = listOf("orange", "apple", "pitch", "avocado")
    when{
//        "orange" in items -> println("ins")
        "pitchs" !in items -> println("not ins")
    }

    items.filter { it.startsWith("a")}
            .sortedBy { it }
            .map { it.toUpperCase() }
            .forEach { println(it) }

    val map = mapOf("a" to 1, "b" to 2, "c" to 3);
    for ((k, v) in map)
    {
        println("$k->$v")
    }
    println(map["a"])

    var files = File("/out").listFiles();
    println(files?.size);
    println(files?.size ?: "empty");

    var value1 = map["av"] ?: println("eee")

    var emails = listOf(null);
    var emailFirst = emails.firstOrNull() ?: "email empty";
    emailFirst.let {
        println(emailFirst.toUpperCase())
    }

    println(getColor("Redp"))

    var r = arrayOfMinusOne(2);
    println(r[0])

    var tc = TestClass();
    with(tc){
        tc.m1()
        tc.m2()
    }

    readFile()

    var number1 = 1000_000
    println(number1)
    var binary = 0b0010
    println(binary)

}

object Sg{
    val name = "ASDFSAF"
}

fun sum(a:Int, b:Int) = a + b

fun maxOf(a:Int , b:Int) : Int{
    if(a > b)
        return b
    else
        return a
}

fun maxOf2(a:Int, b:Int) = if(a > b) a else b

fun getStrLen(obj:Any) : Int?
{
    if(obj is String)
        return obj.length;
    else
        return -1;
}

fun parseInt(a : String) : Int? = 0

fun testLoop(){
    val items = listOf("123", "456", "6789")
    for (s in items)
    {
        println(s)
    }
}

fun whenExp(obj: Any) =
        when(obj){
            1 -> "ONE"
            "hello"->"Greeting"
            is Long->"is long"
            !is String->"not String"
            else ->"unknown"
        }

fun isOnRange(a:Int) = a in 1..100

fun testIteration(){
//    for (x in 1..10 step 2)
//    {
//        print(x)
//    }

    for(x in 9 downTo 0 step 3)
    {
        print(x)
    }
}

fun transformColor(c : String) :Int{
    return when(c){
        "Red" -> 0
        "Blue" ->1
        else->throw IllegalAccessError("not ilegal color")
    }
}

fun getColor(color : String) : Int{
    return try {
        transformColor(color)
    }catch (e: IllegalAccessError)
    {
        return -1
    }
}

fun foo(a: Int){
    val result = if (a == 1){
        "one"
    }else if (a == 2)
    {
        "two"
    }else{
        "other"
    }
}

fun arrayOfMinusOne(size : Int) : IntArray{
    return IntArray(size).apply { fill(-1) }
}

fun singleExpr() = 18

fun readFile(){
    val stream = Files.newInputStream(Paths.get("src/test.txt"))
    stream.buffered().reader().use {
        reader-> println(reader.readText())
    }
}